//
//  RPBaseViewController.m
//  Rippton2.0
//
//  Created by 胡旭辉 on 2019/7/11.
//  Copyright © 2019 胡旭辉. All rights reserved.
//

#import "RPBaseViewController.h"
#import "RPNetWorkErrorView.h"
@interface RPBaseViewController ()<UINavigationControllerDelegate>
@property (nonatomic,strong)UIActivityIndicatorView *spin;
@property (nonatomic,strong)UIImageView *noDateImgView;
@end

@implementation RPBaseViewController



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (!self.isHideNavBar) {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }else{
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#666666"];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    [self beginLogPageView];

}
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];

    [self endLogPageView];
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

-(void)beginLogPageView{
    [MobClick beginLogPageView:NSStringFromClass([self class])];
}


-(void)endLogPageView{
    [MobClick endLogPageView:NSStringFromClass([self class])];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationController.delegate = self;
    
    
    
    
    [self loadUI];
    
    [self setNavBackItem];
    
}




- (void)removeAllHelpView{
  
    NSLog(@"销毁页面");
}

- (void)showNetworkErrorWithNavTitle:(NSString *)navTitle completeBlock:(void (^)(void))completeBlock cancelBlock:(void (^)(void))cancelBlock{
    RPNetWorkErrorView *error = [[RPNetWorkErrorView alloc]initWithNavTitle:navTitle completeBlock:^{
        if (completeBlock) {
            completeBlock();
        }
    } cancelBlock:^{
        if (cancelBlock) {
            cancelBlock();
        }
    }];
    [error showInMainView:self.view];
}


- (void)setNavBackItem{
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_back_tag"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
}


- (void)loadUI{
    

    _spin = [[UIActivityIndicatorView alloc] initWithFrame:(CGRect){0,0,40,40}];
    _spin.center = CGPointMake(kScreenW / 2.0f, kScreenH / 2.5);
    _spin.color = [UIColor lightGrayColor];
    _spin.hidesWhenStopped = YES;
    
    _noDateImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"none"]];
    _noDateImgView.hidden = YES;
    [self.view addSubview:_noDateImgView];

    _nodataNoticeLb = [[UILabel alloc] init];
    _nodataNoticeLb.hidden = YES;
    _nodataNoticeLb.textAlignment = NSTextAlignmentCenter;
    _nodataNoticeLb.backgroundColor = [UIColor clearColor];
    _nodataNoticeLb.textColor = [UIColor lightGrayColor];
    _nodataNoticeLb.text = @"No More Data";
    [self.view addSubview:_nodataNoticeLb];
    
    [_noDateImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-25);
    }];
    
    [_nodataNoticeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_noDateImgView.mas_bottom).offset(10);
        make.centerX.equalTo(_noDateImgView);
    }];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)showNoData{
    _noDateImgView.hidden = NO;
    _nodataNoticeLb.hidden = NO;
    [self.view bringSubviewToFront:_noDateImgView];
    [self.view bringSubviewToFront:_nodataNoticeLb];
    
}
- (void)hideNoData{
    _noDateImgView.hidden = YES;
    _nodataNoticeLb.hidden = YES;
}

- (void)showDataLoadState{
    
    __weak typeof(self)weakSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.view addSubview:weakSelf.spin];
        [weakSelf.spin startAnimating];
    });
}

- (void)hideDataLoadLoadState{
    
    __weak typeof(self)weakSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if(weakSelf.spin.isAnimating){
            [weakSelf.spin stopAnimating];
        }
    });
}
- (void)dealloc
{
    NSLog(@"%s",__func__);
}
@end
